// zh_cjdianc/myAccount/myAccount.js
var a = getApp(),
  util = require("../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    consume: 0,
    logList: [],
    users: [],
    page: 1,
    alert_show: false,
    options_score:'',
    // 导航栏
  },
  consume_0: function() {
    var n = this;
    var user_id = wx.getStorageSync('users').id;
    a.util.request({
      url: "entry/wxapp/newpointsinfo",
      cachetime: "0",
      data: {
        user_id: user_id,
        status: 1
      },
      success: function(t) {
        n.setData({
          logList: t.data.data.points,
          consume: 0,
          page: 1
        });
      }
    });
  },
  consume_1: function() {
    var n = this;
    var user_id = wx.getStorageSync('users').id;
    a.util.request({
      url: "entry/wxapp/newpointsinfo",
      cachetime: "0",
      data: {
        user_id: user_id,
        status: 2
      },
      success: function(t) {
        n.setData({
          logList: t.data.data.points,
          consume: 1,
          page: 1
        });
      }
    });
  },
  alert_cancel: function () {
    this.setData({
      alert_show: false
    })
  },
  scan_code: function () {
    a.scan();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var n = this;
    a.setNavigationBarColor(this), a.pageOnLoad(this);
    console.log(options);
    n.setData({
      options_score:options.score
    })
    if (options.score) {
      n.setData({
        alert_show: true
      })
    }
    var user_id = wx.getStorageSync('users').id;
    var status, consume;
    status = options.status;
    consume = 1;
    console.log(status);
    if (!status) {
      status = 1;
      consume = 0;
    }
    a.util.request({
      url: "entry/wxapp/newpointsinfo",
      cachetime: "0",
      data: {
        user_id: user_id,
        status: status
      },
      success: function(t) {
        n.setData({
          logList: t.data.data.points,
          users: t.data.data.users,
          consume: consume,
          page: 1
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var n = this;
    var user_id = wx.getStorageSync('users').id;
    var consume = n.data.consume;
    var status;
    if (consume == 0) {
      status = 1;
    } else {
      status = 2;
    }
    a.util.request({
      url: "entry/wxapp/newpointsinfo",
      cachetime: "0",
      data: {
        user_id: user_id,
        status: status
      },
      success: function(t) {
        n.setData({
          logList: t.data.data.points,
          page: 1
        });
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
      }
    });
  },
  onReachBottom: function() {
    var n = this;
    var user_id = wx.getStorageSync('users').id;
    var page = n.data.page;
    page = page + 1;
    var consume = n.data.consume;
    var status;
    if (consume == 0) {
      status = 1;
    } else {
      status = 2;
    }
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })
    a.util.request({
      url: "entry/wxapp/newpointsinfo",
      cachetime: "0",
      data: {
        user_id: user_id,
        status: status,
        page: page
      },
      success: function(t) {
        // 回调函数
        var logList = n.data.logList;

        for (var i = 0; i < t.data.data.points.length; i++) {
          logList.push(t.data.data.points[i]);
        }
        // 设置数据
        n.setData({
          logList: logList,
          page: page
        });
        // 隐藏加载框
        wx.hideLoading();
      }
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {//转发
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res)
    }
    return {
      title: '免单王',
      path: '',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})